<template>
  <div class="container">
    <el-container>
      <el-header style="height: 22vh">
        <el-row :gutter="8">
          <el-col :span="10">
            <div class="common-div-card header-main">
              <div class="image">
                <img src="/src/assets/images/report/safe.png" v-if="processStatus" />
                <img src="/src/assets/images/report/danger.png" v-else />
              </div>
              <div class="prod-Line">
                <div class="line">
                  <div class="large-text">1630&nbsp;</div>
                  <div class="normal-text">连退机组</div>
                </div>
                <div class="sub-text">安全运行无违章</div>
              </div>
              <el-divider direction="vertical" style="margin: 0 2rem" />
              <div class="planned-output">
                <div class="number">{{ numberFormat(baseInfo.plannedOutput) }}</div>
                <div class="text">计划产量</div>
              </div>
              <el-divider direction="vertical" style="margin: 0 2rem" />
              <div class="main-info">
                <el-form label-width="auto" label-position="left" class="form-list">
                  <el-form-item label="生产量">
                    <el-text>{{ numberFormat(baseInfo.output) }}</el-text>
                  </el-form-item>
                  <el-form-item label="实际产量">
                    <el-text>{{ numberFormat(baseInfo.achievedOutput * 100) + '%' }}</el-text>
                  </el-form-item>
                  <el-form-item label="直通量">
                    <el-text>{{ numberFormat(baseInfo.throughput) }}</el-text>
                  </el-form-item>
                  <el-form-item label="达成率">
                    <el-text>{{ numberFormat(baseInfo.achievedOutput * 100) + '%' }}</el-text>
                  </el-form-item>
                  <el-form-item label="直通比例">
                    <el-text>{{ numberFormat(baseInfo.FPYratio * 100) + '%' }}</el-text>
                  </el-form-item>
                  <el-form-item label="月产量">
                    <el-text>{{ numberFormat(baseInfo.monthlyOutput) }}</el-text>
                  </el-form-item>
                </el-form>
              </div>
            </div>
          </el-col>
          <el-col :span="7">
            <div class="common-div-card quality">
              <div class="quality-title">质量</div>
              <div class="quality-process">
                <div class="process-with-title">
                  <el-progress type="dashboard" :percentage="numberFormat(baseInfo.wasteRatePlan)" :color="colors" />
                  <div class="process-title">
                    <span>废次率指标</span>
                  </div>
                </div>
                <div class="process-with-title">
                  <el-progress type="dashboard" :percentage="numberFormat(baseInfo.wasteRateDaily)" :color="colors" />
                  <div class="process-title">
                    <span>当日废次率</span>
                  </div>
                </div>
                <div class="process-with-title">
                  <el-progress type="dashboard" :percentage="numberFormat(baseInfo.wasteRateMonthly)" :color="colors" />
                  <div class="process-title">
                    <span>月累计废次率</span>
                  </div>
                </div>
              </div>
            </div>
          </el-col>
          <el-col :span="7">
            <div class="common-div-card yield">
              <div class="yield-title">成材率</div>
              <div class="yield-content">
                <div class="plan">
                  <div class="image">
                    <!-- <el-image src="/src/assets/images/report/plan.png" /> -->
                    <img src="/src/assets/images/report/plan.png" />
                  </div>
                  <div class="text-info">
                    <div class="status">
                      <div>计划&nbsp;</div>
                      <div v-if="baseInfo.yieldRateResult == '超欠'"><el-tag type="danger">超欠</el-tag></div>
                      <div v-else><el-tag type="success">正常</el-tag></div>
                    </div>
                    <div class="yield-text">{{ numberFormat(baseInfo.yieldRatePlan * 100) + '%' }}</div>
                  </div>
                </div>
                <div class="actual">
                  <div class="image">
                    <!-- <el-image src="/src/assets/images/report/actual.png" /> -->
                    <img src="/src/assets/images/report/actual.png" />
                  </div>
                  <div class="text-info">
                    <div class="status">
                      <div>日实际&nbsp;</div>
                      <div v-if="baseInfo.yieldRateResultValue == '超欠'"><el-tag type="danger">超欠</el-tag></div>
                      <div v-else><el-tag type="success">正常</el-tag></div>
                    </div>
                    <div class="yield-text">{{ numberFormat(baseInfo.yieldRateActual * 100) + '%' }}</div>
                  </div>
                </div>
              </div>
            </div>
          </el-col>
        </el-row>
      </el-header>
      <el-main>
        <el-row :gutter="8">
          <el-col :span="8">
            <el-card>
              <template #header>
                <div><span>生产运行情况</span></div>
                <div>
                  <span>影响产量合计</span>&nbsp;&nbsp;<el-text>{{ numberFormat(prodOpSum) }}</el-text>
                </div>
              </template>
              <div class="card-body">
                <el-empty description="暂无数据" v-if="prodOpStatus.length == 0" />
                <div v-for="(item, index) in prodOpStatus" :key="item.period" class="status-item">
                  <div class="status-item-content">
                    <div class="circle">{{ index + 1 }}</div>
                    <div class="content-lit">
                      <div class="column">
                        <div><span>停机时间段</span></div>
                        <div>
                          <el-text>{{ item.period }}</el-text>
                        </div>
                      </div>
                      <div class="column">
                        <div><span>停机时长（分）</span></div>
                        <div>
                          <el-text>{{ item.duration }}</el-text>
                        </div>
                      </div>
                      <div class="column">
                        <div><span>影响产量（吨）</span></div>
                        <div>
                          <el-text>{{ item.affectedOutput }}</el-text>
                        </div>
                      </div>
                      <div class="column">
                        <div><span>停机（降速）说明</span></div>
                        <div style="display: flex; align-items: center; gap: 0.5rem">
                          <el-popover trigger="hover" placement="bottom" width="300" popper-class="stop-remark-popover" :content="item.stopRemark">
                            <template #reference>
                              <div><img src="/src/assets/images/report/icon-tips.png" /></div>
                            </template>
                          </el-popover>
                          <el-text class="ellipsis">{{ item.stopRemark }}</el-text>
                        </div>
                      </div>
                    </div>
                  </div>
                  <el-divider />
                </div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="8">
            <el-card>
              <template #header>
                <div><span>降级情况</span></div>
                <div>
                  <span>降级量合计</span>&nbsp;&nbsp;<el-text>{{ numberFormat(downgradeSum) }}</el-text>
                </div>
              </template>
              <div class="card-body">
                <el-empty description="暂无数据" v-if="downgrade.length == 0" />
                <div v-for="(item, index) in downgrade" :key="item.defectName" class="status-item">
                  <div class="status-item-content">
                    <div class="circle">{{ index + 1 }}</div>
                    <div class="content">
                      <div class="column" style="min-width: 12vw">
                        <div style="display: flex">
                          <span>降级缺陷名称&nbsp;</span>
                          <el-tag type="primary" size="small" v-if="item.downType == 0">连退</el-tag>
                          <el-tag type="primary" size="small" v-if="item.downType == 1">精整</el-tag>
                        </div>
                        <div>
                          <el-text>{{ item.defectName }}</el-text>
                        </div>
                      </div>
                      <div class="column">
                        <div><span>降级量</span></div>
                        <div>
                          <el-text>{{ item.amount }}</el-text>
                        </div>
                      </div>
                      <div class="column">
                        <div><span>归户</span></div>
                        <div>
                          <el-text>{{ item.household }}</el-text>
                        </div>
                      </div>
                      <div class="column" style="min-width: 7vw">
                        <div><span>钢种规格</span></div>
                        <div>
                          <el-text>{{ item.steelGrade }} &nbsp;{{ item.think }} &nbsp;{{ item.width }}</el-text>
                        </div>
                      </div>
                      <div class="column">
                        <div><span>停机（降速）说明</span></div>
                        <div>
                          <el-text>{{ item.stopRemark }}</el-text>
                        </div>
                      </div>
                    </div>
                  </div>
                  <el-divider />
                </div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="8">
            <el-card>
              <template #header>
                <div><span>封闭量</span></div>
                <div>
                  <span>封闭量合计</span>&nbsp;&nbsp;<el-text>{{ numberFormat(closedSum) }}</el-text>
                </div>
              </template>
              <div class="card-body">
                <el-empty description="暂无数据" v-if="closed.length == 0" />
                <div v-for="(item, index) in closed" :key="item.defectName" class="status-item">
                  <div class="status-item-content">
                    <div class="circle">{{ index + 1 }}</div>
                    <div class="content">
                      <div class="column" style="min-width: 12vw">
                        <div><span>封闭缺陷&nbsp;</span></div>
                        <div>
                          <el-text>{{ item.defectName }}</el-text>
                        </div>
                      </div>
                      <div class="column">
                        <div><span>封闭量</span></div>
                        <div>
                          <el-text>{{ item.amount }}</el-text>
                        </div>
                      </div>
                      <div class="column">
                        <div><span>归户</span></div>
                        <div>
                          <el-text>{{ item.household }}</el-text>
                        </div>
                      </div>
                      <div class="column" style="min-width: 7vw">
                        <div><span>钢种规格</span></div>
                        <div>
                          <el-text>{{ item.steelGrade }} &nbsp;{{ item.think }} &nbsp;{{ item.width }}</el-text>
                        </div>
                      </div>
                      <div class="column">
                        <div><span>停机（降速）说明</span></div>
                        <div>
                          <el-text>{{ item.stopRemark }}</el-text>
                        </div>
                      </div>
                    </div>
                  </div>
                  <el-divider />
                </div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </el-main>
    </el-container>
  </div>
</template>
<script lang="ts" setup>
  import { ref, onMounted } from 'vue';
  import { cal1630Api } from '/@/api/report';

  const colors = [
    { color: '#f56c6c', percentage: 20 },
    { color: '#e6a23c', percentage: 40 },
    { color: '#5cb87a', percentage: 60 },
    { color: '#1989fa', percentage: 80 },
    { color: '#6f7ad3', percentage: 100 },
  ];
  const numberFormat = (number: any) => {
    return number == undefined || isNaN(number) ? '0' : Number(parseFloat(number).toFixed(2));
  };

  const baseInfo = ref<any>({
    output: '', // 生产量
    throughput: '', // 直通量
    FPYratio: '', // 直通比例
    plannedOutput: '', // 计划产量
    actualOutput: '', // 实际产量
    achievedOutput: '', // 达成量
    monthlyOutput: '', // 月产量
    yieldRatePlan: '', // 成才率-计划
    yieldRateActual: '', // 成才率-实际
    yieldRateResult: '', // 成材率-结果
    yieldRateResultValue: '', // 成材率-结果值
    wasteRatePlan: '', // 废次率-指标
    wasteRateDaily: '', // 当日废次率
    wasteRateMonthly: '', // 月累计废次率
  });

  const processStatus = ref(true);
  const prodOpStatus = ref<any>([]);
  const prodOpSum = ref();

  const downgrade = ref<any>([
    // {
    //   defectName: '氧化色（带钢表面出现异色氧化层）',
    //   amount: '1.88',
    //   household: '连退机械',
    //   steelGrade: 'DC01-TS',
    //   think: '0.9',
    //   width: '1879',
    //   stopRemark: '停机原因11111111111111111',
    //   downType: 0,
    // },
    // {
    //   defectName: '氧化色（带钢表面出现异色氧化层）',
    //   amount: '1.88',
    //   household: '连退机械',
    //   steelGrade: 'DC01-TS',
    //   think: '0.9',
    //   width: '1879',
    //   stopRemark: '停机原因2222222222222222222',
    //   downType: 0,
    // },
  ]);
  const downgradeSum = ref(0);

  const closed = ref<any>([]);

  const closedSum = ref(0);

  const statusImage = () => {
    let status = 'safe';
    return `/src/assets/images/report/${status}.png`;
  };

  const remarkClick = (remark: string) => {};

  // 基本信息
  const getBaseInfo = async () => {
    try {
      const { data } = await cal1630Api.getBaseInfo();
      if (data.status != 2000) {
        throw new Error('数据查询失败：' + data.message);
      }
      // 确保基础信息对象存在
      if (!baseInfo.value) baseInfo.value = {};
      const {
        shengchanl, // 生产量
        zhitongl, // 直通量
        zhitongbl, // 直通比例
        jihuacl, // 计划产量
        shijicl, // 实际产量
        dachengl, // 达成量
        yuechanl, // 月产量
        chengcaijh, // 成才率-计划
        chengcaisj, // 成才率-实际
        chengcaijg, // 成材率-结果
        chengcaijgz, // 成材率-结果值
        feicilzb, // 废次率-指标
        dangrifcl, // 当日废次率
        yueleijfcl, // 月累计废次率
      } = data.data;

      // 单次赋值提升性能
      Object.assign(baseInfo.value, {
        output: shengchanl,
        throughput: zhitongl,
        FPYratio: zhitongbl,
        plannedOutput: jihuacl,
        actualOutput: shijicl,
        achievedOutput: dachengl,
        monthlyOutput: yuechanl,
        yieldRatePlan: chengcaijh,
        yieldRateActual: chengcaisj,
        yieldRateResult: chengcaijg,
        yieldRateResultValue: chengcaijgz,
        wasteRatePlan: feicilzb,
        wasteRateDaily: dangrifcl,
        wasteRateMonthly: yueleijfcl,
      });
    } catch (e: any) {
      console.error('获取基本信息失败：' + e.message);
    }
  };

  // 封闭量
  const getCloseList = async () => {
    try {
      const res = await cal1630Api.getCloseList();
      if (res.data.status != 2000) {
        throw new Error('数据查询失败：' + res.data.message);
      }
      closed.value = res.data.data.map((item: any) => ({
        defectName: item.mingcheng,
        amount: item.jiangjil,
        household: item.guihu,
        steelGrade: item.guigeCol1,
        think: item.guigeCol2,
        width: item.guigeCol3,
        stopRemark: item.yuanyin,
      }));
      closedSum.value = res.data.data.reduce((acc: any, cur: any) => acc + cur.jiangjil, 0);
    } catch (e: any) {
      console.error('获取封闭量信息失败：' + e.message);
    }
  };

  // 降级连退精整
  const getDownList = async () => {
    try {
      const res = await cal1630Api.getDownList();
      if (res.data.status != 2000) {
        throw new Error('数据查询失败：' + res.data.message);
      }
      downgrade.value = res.data.data.map((item: any) => ({
        defectName: item.mingcheng,
        amount: item.jiangjil,
        household: item.guihu,
        steelGrade: item.guigeCol1,
        think: item.guigeCol2,
        width: item.guigeCol3,
        stopRemark: item.yuanyin,
        downType: item.downType,
      }));
      downgradeSum.value = res.data.data.reduce((acc: any, cur: any) => acc + cur.jiangjil, 0);
    } catch (e: any) {
      console.error('获取降级连退信息失败：' + e.message);
    }
  };

  // 格式化数值
  const formatNum = (value: any) => {
    if (value || value == '' || value == null || isNaN(value)) return '0.00';
    return value.toFixed(2);
  };

  // 生产运行情况
  const getRunList = async () => {
    try {
      const res = await cal1630Api.getRunList();
      if (res.data.status != 2000) {
        throw new Error('数据查询失败：' + res.data.message);
      }
      prodOpStatus.value = res.data.data.map((item: any) => ({
        period: item.shiduan.replace('：', ':'),
        duration: formatNum(item.shichang),
        affectedOutput: formatNum(item.yingxiangcl),
        stopRemark: item.shuoming,
      }));
      prodOpSum.value = res.data.data.reduce((acc: any, cur: any) => acc + cur.yingxiangcl, 0);
    } catch (e: any) {
      console.error('获取生产运行情况失败：' + e.message);
    }
  };

  const fetchData = () => {
    getBaseInfo();
    getCloseList();
    getDownList();
    getRunList();
  };

  onMounted(() => {
    fetchData();
  });
</script>
>
<style scoped lang="less">
  // 表格和选择框共同样式
  @border-radius: 8px;
  @primary-color: #2c69e8;
  @box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  @box-shadow-hover: 0 8px 32px rgba(0, 0, 0, 0.12);
  @border:1px solid #dcdfe6;
  @boder-bule:1px solid #2c69e8;
  @text-color: #333333;

  .container {
    height: 100vh;
    width: 99vw;
    background-color: #fff;
    border-radius: @border-radius;
  }

  .common-div-card {
    box-shadow: @box-shadow;
    transition: 0.3s;
    border-radius: @border-radius;
    border: @border;

    // 悬停状态
    &:hover {
      box-shadow: @box-shadow-hover;
    }
  }

  // -------------------------------------------- header 样式 ---------------------------------------------------------
  .header-main {
    margin-top: 1vh;
    width: 100%;
    height: 20vh;
    padding: 1rem;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .prod-Line {
    color: @text-color;
    display: flex;
    flex-direction: column;
    margin-left: -1vw;
    gap: 0.5rem;
    .line {
      display: flex;
      align-items: baseline;
    }
    .large-text {
      font-size: 1.6rem;
      font-weight: 700;
    }
    .normal-text {
      font-size: 1.2rem;
      font-weight: 450;
    }
  }

  .planned-output {
    color: @text-color;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    .number {
      font-size: 1.6rem;
      font-weight: 700;
    }
  }

  .main-info {
    display: flex;
    align-items: center;
    margin-top: 1rem;

    .el-text {
      color: @text-color !important;
      font-weight: 700;
    }

    .form-list {
      display: grid;
      grid-template-columns: repeat(2, 1fr);
    }
  }

  // ----------------- 质量 ---------------------
  .quality {
    margin-top: 1vh;
    width: 100%;
    height: 20vh;
    padding: 1rem;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
  }

  .yield-title,
  .quality-title {
    font-size: 1.2rem;
    font-weight: 500;
    width: 100%;
    margin-top: 0.5rem;
    height: 1.5rem;
  }

  .quality-process {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    justify-content: center;
    align-items: center;
    gap: 2rem;
    margin-bottom: 1rem;

    .process-with-title {
      display: flex;
      flex-direction: column;

      .process-title {
        text-align: center;
      }
    }
  }

  // ----------------- 成材率 ---------------------
  .yield {
    margin-top: 1vh;
    width: 100%;
    height: 20vh;
    padding: 1rem;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
  }

  .yield-content {
    width: 100%;
    height: 100%;
    justify-content: center;
    align-items: center;
    display: flex;
    gap: 2rem;
    justify-content: space-around;

    .actual,
    .plan {
      height: 100%;
      width: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      gap: 1rem;
    }

    .text-info {
      display: flex;
      flex-direction: column;
      align-items: center;

      .status {
        display: flex;
        align-items: center;
      }

      .yield-text {
        margin-top: 0.5rem;
        font-size: 1.2rem;
        font-weight: 500;
      }
    }
  }

  // --------------------------------------------------------------------------

  :deep(.el-card__header) {
    display: flex;
    justify-content: space-between;

    .el-text {
      color: #e66;
      font-weight: 700;
    }
  }

  .card-body {
    height: 60vh;
    overflow: auto;
    overflow-y: auto;
    font-size: 0.9rem;

    // /* 兼容 Firefox */
    scrollbar-width: thin;
    scrollbar-color: #b8dbff transparent; // #f1f1f1

    .status-item {
      display: flex;
      align-items: center;
      flex-direction: column;
      justify-content: flex-start;

      .status-item-content {
        width: 100%;
        display: flex;
        align-items: center;
        gap: 1.4rem;
      }

      .content {
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        gap: 1.4rem;

        .column:last-child {
          grid-column: 1 / -1;
          // margin-top: 0.5rem;
        }
      }

      .content-lit {
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        gap: 1.4rem;
      }

      .column {
        display: flex;
        flex-direction: column;
        gap: 1rem;

        .el-text {
          font-weight: 700;
        }
      }
    }
  }

  .circle {
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #e5f1fe;
    border-radius: 50%;
    width: 1.5rem;
    height: 1.5rem;
    min-width: 1.5rem;
    min-height: 1.5rem;
    flex-shrink: 0;
    color: #0878ff;
    font-weight: 600;
    font-size: 0.8rem;
  }

  .ellipsis {
    white-space: nowrap; /* 防止文本换行 */
    overflow: hidden; /* 隐藏溢出的文本 */
    text-overflow: ellipsis; /* 显示省略号以代表被隐藏的文本 */
    max-width: 120px; /* 根据需要调整最大宽度 */
  }
</style>
<style>
  .el-popover.stop-remark-popover {
    border: 1px solid #b8dbff;
    background-color: #fff;
    color: #333;

    .el-popper__arrow::before,
    .el-popper__arrow::after {
      border: 1px solid #0878ff;
    }
  }
</style>
